package com.heihei.security.config;

import com.heihei.security.interceptor.SimpleAuthentationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import sun.java2d.pipe.SpanShapeRenderer;

import javax.annotation.Resource;

/**
 * ServletContext配置
 *
 * @author duanhengfei
 * @version 1.0
 * @date 2020/6/26 21:52
 * @since JDK1.8
 */
@Configuration  //相当于springmvc.xml文件
@EnableWebMvc
@ComponentScan(basePackages = "com.heihei.security",
        includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = Controller.class)})
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private SimpleAuthentationInterceptor simpleAuthentationInterceptor;

    //视图解析器
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //spring security默认提供的登录页面  redirect:/login
        registry.addViewController("/").setViewName("login");
    }

    /**
     * 添加拦截器
     *
     * @author duanhengfei
     * @date 2020年07月04日 10:15:57
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截器，并且只有形如  /resource/**  的路径才拦截
        registry.addInterceptor(simpleAuthentationInterceptor).addPathPatterns("/resource/**");
    }
}